import navToNext from "../../../wxjs/navToNext";
const app = getApp();

var data = {
  xhx_images: app.xhx_images,
  xhx_route: app.xhx_route,
  mainColor:app.mainColor,
};

var currentPage = {
  data,
  onLoad,
  addrDetails,//地址详情
  changeRegion,//地址选择
  addrConfirm,//确定地址
};

var page = Object.assign({},navToNext,currentPage);

Page(page);

function onLoad(options) {
  const that = this;
  const uuid = options && options.uuid || "";

  that.setData({
    uuid
  })

  if(uuid){
    that.addrDetails();
  }
}

function addrDetails(){
  const that = this;
  const uuid = that.data.uuid;

  app.middleware.requestAjax({
    serAddr: app.serAddrs.addrDetails,
    data: {uuid},
    success: function (res) {
      that.setData(res.data || {});
    },
    fail: function (res) {
      app.middleware.showToast(res && res.msg || "请求失败");
    }
  })
}

function changeRegion(event){
  const that = this;
  const address = event.detail.value;
  that.setData({
    address
  })
}

function addrConfirm(event){
  const that = this;
  const uuid = that.data.uuid || "";
  const form = event.detail.value;
  let key = "addrAdd";

  form.uuid = uuid;
  form.address = form.address.join('');
  form.status = form.status?1:0;

  if (uuid) key ="addrUpdate"
  
  app.middleware.requestAjax({
    serAddr: app.serAddrs[key],
    data: form,
    success: function (res) {
      that.setData(res.data || {});
      var page = getCurrentPages()[getCurrentPages().length - 2];
      if (page.pageListInit) page.pageListInit();
      setTimeout(() => {
        wx.navigateBack();
      }, 2000)
    },
    complete: function (res) {
      app.middleware.showToast(res && res.msg || "请求失败");
    }
  })
}